﻿/* Copyright Info */

namespace("Sieena.Objects");

(function ($, undefined) { 

Sieena.Objects.Clock = (function() { 

    var aSecond,
     dtDate,
     iCurrSecond,
     iHourRotation,
     iMinuteRotation,
     iStepSize,
     iTimerAnimate,
     iTimerUpdate;
    
    function animate()
    {
        if (aSecond.length > 0) {
            rotate("analogsecond", aSecond[0]);
            aSecond = aSecond.slice(1);
        }
    }

    function getHour()
    {
        var iHours = dtDate.getHours();
        if (iHours > 11) {
            iHours -= 12;
        }
        return Math.round((dtDate.getHours() * 30) + (dtDate.getMinutes() / 2) + (dtDate.getSeconds() / 120));
    }

    function getMinute()
    {
        return Math.round((dtDate.getMinutes() * 6) + (dtDate.getSeconds() / 10));
    }

    function init()
    {
        aSecond = [];
        dtDate = new Date();
        iCurrSecond = -1;
        iHourRotation = -1;
        iMinuteRotation = -1;
        iStepSize = 10;
        iTimerAnimate = setInterval(function() { animate(); }, 20);
        iTimerUpdate = setInterval(function() { update(); }, 1000);

        $("#analoghour").attr("src", "/Content/Images/analoghours.png");
        $("#analogminute").attr("src", "/Content/Images/analogminutes.png");
        $("#analogsecond").attr("src", "/Content/Images/analogseconds.png");

        iHourRotation = getHour();
        rotate("analoghour", iHourRotation);

        iMinuteRotation = getMinute();
        rotate("analogminute", iMinuteRotation);

        iCurrSecond = dtDate.getSeconds();
        rotate("analogsecond", (6 * iCurrSecond));        
    }

    function rotate(sID, iDeg)
    {
        var sCSS = ("rotate(" + iDeg + "deg)");
        $("#" + sID).css({ '-moz-transform': sCSS, '-o-transform': sCSS, '-webkit-transform': sCSS });
    }

    function stepSize(iTo, iFrom)
    {
        var iAnimDiff = (iFrom - iTo);
        if (iAnimDiff > 0) {
            iAnimDiff -= 360;
        }
        return iAnimDiff / iStepSize;
    }

    function update()
    {
        dtDate = new Date();

        // hours
        var iTemp = getHour();
        if (iHourRotation != iTemp) {
            iHourRotation = iTemp;
            rotate("analoghour", iTemp);
        }

        // minutes
        iTemp = getMinute();
        if (iMinuteRotation != iTemp) {
            iMinuteRotation = iTemp;
            rotate("analogminute", iTemp);
        }

        // seconds
        if (iCurrSecond != dtDate.getSeconds()) {
            var iRotateFrom = (6 * iCurrSecond);
            iCurrSecond = dtDate.getSeconds();
            var iRotateTo = (6 * iCurrSecond);

            // push steps into array
            var iDiff = stepSize(iRotateTo, iRotateFrom);
            for (var i = 0; i < iStepSize; i++) {
                iRotateFrom -= iDiff;
                aSecond.push(Math.round(iRotateFrom));
            }
        }

    }

    return {
        Init : init
    };

});

})(jQuery);

